package com.zlc.platform.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.annotation.LogSaveAnnotation;
import com.zlc.common.common.constant.PlatConstant;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.common.exception.BusinessException;
import com.zlc.common.core.resource.model.entity.MayanPlatSystem;
import com.zlc.common.core.resource.model.entity.MayanPlatSystemResource;
import com.zlc.common.core.resource.model.vo.MayanPlatSystemResourceVo;
import com.zlc.common.core.resource.model.vo.MayanPlatSystemVo;
import com.zlc.common.model.resource.ResourceEntity;
import com.zlc.common.model.resource.SystemPageRequest;
import com.zlc.common.util.AssertUtils;
import com.zlc.platform.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import java.util.List;
import java.util.Map;

@Api(tags = "系统资源相关")
@RestController
@Slf4j
@RequestMapping("/api/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @ApiOperation(value = "查询所有系统")
    @GetMapping(value = "/queryAllSystem")
    public List<MayanPlatSystemVo> queryAllSystem() {
        return resourceService.queryAllSystem();
    }

    @ApiOperation(value = "查询所有系统")
    @PostMapping(value = "/querySystem")
    public PageVo<MayanPlatSystemVo> querySystem(@RequestBody SystemPageRequest request) {
        return resourceService.querySystem(request);
    }

    @ApiOperation(value = "添加系统")
    @PostMapping(value = "/addSystem")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "添加系统", catalog = "权限管理/系统管理")
    public void addSystem(@RequestBody MayanPlatSystemVo system) {
        resourceService.addSystem(system);
    }

    @ApiOperation(value = "修改系统")
    @PostMapping(value = "/updateSystem")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "修改系统", catalog = "权限管理/系统管理")
    public void updateSystem(@RequestBody MayanPlatSystemVo system) {
        resourceService.updateSystem(system);
    }

    @ApiOperation(value = "删除系统")
    @PostMapping(value = "/removeSystem")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "删除系统", catalog = "权限管理/系统管理")
    public void removeSystem(@RequestBody Map<String,String> map) {
        resourceService.removeSystem(map.get("id"));
    }


    @ApiOperation(value = "查询系统资源")
    @GetMapping(value = "/queryResource")
    public ResourceEntity queryResource(@RequestParam(value = "systemCode") String systemCode) {
         return resourceService.queryResource(systemCode);
    }

    /**
     * 查询所有系统资源
     */
    @ApiOperation(value = "查询所有系统资源")
    @GetMapping(value = "/queryAllResource")
    public List<ResourceEntity> queryAllResource(){
        return resourceService.queryAllResource();
    }


    /**
     * 添加资源
     */
    @ApiOperation(value = "添加资源")
    @PostMapping(value = "/addResource")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "添加资源", catalog = "权限管理/资源管理")
    public MayanPlatSystemResourceVo addResource(@RequestBody MayanPlatSystemResourceVo resourceVo){
        return resourceService.addResource(resourceVo);
    }


    @ApiOperation(value = "查询单个资源")
    @GetMapping(value = "/queryByResourceId")
    public MayanPlatSystemResourceVo queryByResourceId(@RequestParam(value = "resourceId") String resourceId) {
        return resourceService.queryByResourceId(resourceId);
    }


    /**
     * 修改资源
     */
    @ApiOperation(value = "修改资源")
    @PostMapping(value = "/updateResource")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "修改资源", catalog = "权限管理/资源管理")
    public MayanPlatSystemResourceVo  updateResource(@RequestBody MayanPlatSystemResourceVo resourceVo){
        return resourceService.updateResource(resourceVo);
    }


    /**
     * 删除资源
     */
    @ApiOperation(value = "删除资源")
    @PostMapping(value = "/removeResource")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "删除资源", catalog = "权限管理/资源管理")
    public void removeResource(@RequestBody Map<String,String> map){
        resourceService.removeResource(map.get("resourceId"));
    }
}
